using UnityEngine;
using System.Collections;
using System;
using DanielSig.Sorcery;

[Serializable]
public class PropertyAssignment
{
	public String PropertyName;
	public PropertyType Type;
	
	public bool BooleanValue;
	public float FloatValue;
	public int IntValue;
	public String StringValue;
	public Vector2 Vector2Value;
	public Vector3 Vector3Value;
	public Quaternion QuaternionValue;
	public GameObject GameObjectValue;
	public Component ComponentValue;
}

public enum PropertyType
{
	Boolean,
	Float,
	Int,
	String,
	Vector2,
	Vector3,
	Quaternion,
	GameObject,
	Component,
	Void
}
public enum MessageReceiver
{
	TargetOnly,
	Parents,
	Children
}

[RequireComponent(typeof(Collider))]
public class MessageTrigger : MonoBehaviour
{
	public bool DestroyOnTriggerEnter;
	public SendMessageOptions Options;
	public MessageReceiver Receivers;
	
	public PropertyAssignment[] MessagesToSend;
	
	void OnTriggerEnter(Collider other)
	{
		foreach(PropertyAssignment a in MessagesToSend)
		{
			object propValue = null;
			switch(a.Type)
			{
				case PropertyType.Boolean:
					propValue = a.BooleanValue;
					break;
				case PropertyType.Float:
					propValue = a.FloatValue;
					break;
				case PropertyType.Int:
					propValue = a.IntValue;
					break;
				case PropertyType.String:
					propValue = a.StringValue;
					break;
				case PropertyType.Vector2:
					propValue = a.Vector2Value;
					break;
				case PropertyType.Vector3:
					propValue = a.Vector3Value;
					break;
				case PropertyType.Quaternion:
					propValue = a.QuaternionValue;
					break;
				case PropertyType.GameObject:
					propValue = a.GameObjectValue;
					break;
				case PropertyType.Component:
					propValue = a.ComponentValue;
					break;
				case PropertyType.Void:
					propValue = null;
					break;
			}
			if(a.Type == PropertyType.Void)
			{
				if(Receivers == MessageReceiver.TargetOnly)
					other.SendMessage(a.PropertyName, Options);
				else if(Receivers == MessageReceiver.Children)
					other.BroadcastMessage(a.PropertyName, Options);
				else
					other.SendMessageUpwards(a.PropertyName, Options);
			}
			else if(Receivers == MessageReceiver.TargetOnly)
				other.SendMessage(a.PropertyName, propValue, Options);
			else if(Receivers == MessageReceiver.Children)
				other.BroadcastMessage(a.PropertyName, propValue, Options);
			else
				other.SendMessageUpwards(a.PropertyName, propValue, Options);
		}
		if(DestroyOnTriggerEnter)
			Destroy(gameObject);
	}
}

